clear
set more off, perm
cd /Users/zachbrown/Projects/PriceTransparency/Data/

// Use membership file
	forval yr = 2005(1)2014 {
		disp "year: `yr'"
		tempfile tmpdata
		shell nice gunzip Raw/members/member_month_`yr'_clean.dta -c > `tmpdata'
		append using `tmpdata', keep(member_key yearmo coverage_medical primary_ins payercode std_product_type insurance_type tier member_zip)
	}

// Keep only medical coverage
keep if coverage_medical==1

// Merge on payer info
merge m:1 payercode using build/payers_clean.dta
drop if _merge==2
drop _merge

// Covert payercode to numeric
egen payer_id = group(payercode)
drop payercode

sort member_key yearmo coverage_medical primary_ins
duplicates drop member_key yearmo coverage_medical primary_ins, force

// Merge on start and end data from collapsed membership file
merge m:1 member_key using Raw/members/mm_member_list.dta
drop if _merge==2
drop _merge

compress
gsave build/members_clean.dta, replace


// Get list of months with claims for each member
preserve
clear
global year_min 2005
global year_max 2014
global keep_vars "member_key from_date"

forval yr = $year_min(1)$year_max {
	disp "year: `yr'"
	tempfile tmpdata
	shell nice gunzip Raw/med_clm/med_clm_`yr'_clean.dta -c > `tmpdata'
	append using `tmpdata', keep($keep_vars)
}

gen yearmo = year(from_date)*100 + month(from_date)

contract yearmo member_key
drop _freq
sort yearmo member_key

save build/member_claim_month_list.dta, replace
restore

merge m:1 member_key yearmo using build/member_claim_month_list.dta
drop if _merge==1
drop _merge

gsave build/members_clean_small.dta, replace


